/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.transform_t2d.var;

import java.util.Properties;
import org.opu.doc2db.document.datadocument.TextBlock;
import org.opu.vcforms.template.bean.Var;
import org.opu.vcforms.transform_t2d.Processor;
import org.opu.vcforms.transform_t2d.SystemConfiguration;
import org.opu.vcforms.transform_t2d.content.ContentAPI;
import org.opu.vcforms.transform_t2d.content.ContentSqlPreparator;
import static org.opu.vcforms.transform_t2d.Logger.*;

/**
 *
 * @author yura
 */
public class VarProcessor implements Processor<TextBlock, Var>{

    private ContentAPI content;
    private Properties systemSettings;
    private SystemConfiguration systemConfiguration;

    public VarProcessor(ContentAPI content, Properties systemSettings,
            SystemConfiguration systemConfiguration) {
        
        this.content = content;
        this.systemSettings = systemSettings;
        this.systemConfiguration = systemConfiguration;
        out(var(SPACE) + "Var.<created>");
    }

    /**
     *  Transform variable to text block.
     * @param var variable with data.
     * @return text block with data according with variable.
     * @throws UnsupportedOperationException if variable type is calc
     * @throws IllegalArgumentException if any problems with getting data
     * from content ({@link ContentSqlPreparator#parseQuery(java.lang.String)).
     */
    @Override
    public TextBlock transform(Var var) throws UnsupportedOperationException,
            IllegalArgumentException {
        out(var(SPACE) + "Var.transform");

        TextBlock result = new TextBlock();
        String text = var.getText();

        out(var(SPACE) + "Var.transform: Var type is " + var.getType());
        if (Var.TYPE_CONTENT.equals(var.getType())){
            if (text.startsWith(Var.REGULAT) && text.length() > 1){
                out(var(SPACE) + "Var.transform: text type is regular: " + text);
                ContentSqlPreparator prep = new ContentSqlPreparator(content);
                String substring = text.substring(1, text.length());
                String data = prep.parseQuery(substring);
                setVarText(result, var, data);
            } else if (Var.CONTENT_CALL_NEXT.equals(text)){
                out(var(SPACE) + "Var.transform: text type is 'next': " + text);
                String data = content.next();
                setVarText(result, var, data);
            } else if (Var.CONTENT_CALL_STEP.equals(text)){
                out(var(SPACE) + "Var.transform: text type is 'step': " + text);
                content.step();
                result.setText("");
                out(var(SPACE) + "Var.transform: text is empty");
            } else if (Var.CONTENT_CALL_ITERATION.equals(text)){
                out(var(SPACE) + "Var.transform: text type is 'iteration': " + text);
                int data = content.iteration();
                setVarText(result, var, data);
            } else {
                err(var(SPACE) + "Var.transform: wrong variable content argument " + text);
                setVarText(result, var, text);
            }
        }

        else if (Var.TYPE_CALC.equals(var.getType())){
            throw new UnsupportedOperationException("calc action, not supported yet.");
        }

        else if (Var.TYPE_SETTINGS.equals(var.getType())){
            if(systemConfiguration.isSystemRequest(text)){
                out(var(SPACE) + "Var.transform: system call: " + text);
                String data = systemConfiguration.resolveSystemRequest(text);
                setVarText(result, var, data);
            } else {
                out(var(SPACE) + "Var.transform: setting is " + text);
                SettingsVar sv = new SettingsVar(systemSettings);
                String data = sv.data(text);
                setVarText(result, var, data);
            }
        }

         //probably it`s text.
        else {
            if (! Var.TYPE_TEXT.equals(var.getType())){// if it`s not.
                err(var(SPACE) + "Var.transform: wrong variable type - " + var.getType());
            }
            setVarText(result, var, text);
        }

        return result;
    }

    private void setVarText(TextBlock result, Var var, Object data) {
        String ret;
        try {
            ret = String.format(var.getFormat(), data);
        } catch (Exception e) {
            err(var(SPACE) + "Var.setVarText: Set fromat text exception: " + e);
            ret = String.valueOf(data);
        }
        result.setText(ret);
        out(var(SPACE) + "Var.setVarText: text - " + ret);
    }

}
